package org.dromara.system.controller.business;

import java.util.List;

import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.web.core.BaseController;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.system.domain.vo.SalesDataVo;
import org.dromara.system.domain.bo.SalesDataBo;
import org.dromara.system.service.ISalesDataService;
import org.dromara.common.mybatis.core.page.TableDataInfo;

/**
 * 销售数据信息
 *
 * @author joker
 * @date 2025-05-05
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/business/salesData")
public class SalesDataController extends BaseController {

    private final ISalesDataService salesDataService;

    /**
     * 查询销售数据信息列表
     */
    @SaCheckPermission("business:salesData:list")
    @GetMapping("/list")
    public TableDataInfo<SalesDataVo> list(SalesDataBo bo, PageQuery pageQuery) {
        return salesDataService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出销售数据信息列表
     */
    @SaCheckPermission("business:salesData:export")
    @Log(title = "销售数据信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(SalesDataBo bo, HttpServletResponse response) {
        List<SalesDataVo> list = salesDataService.queryList(bo);
        ExcelUtil.exportExcel(list, "销售数据信息", SalesDataVo.class, response);
    }

    /**
     * 获取销售数据信息详细信息
     *
     * @param salesId 主键
     */
    @SaCheckPermission("business:salesData:query")
    @GetMapping("/{salesId}")
    public R<SalesDataVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long salesId) {
        return R.ok(salesDataService.queryById(salesId));
    }

    /**
     * 新增销售数据信息
     */
    @SaCheckPermission("business:salesData:add")
    @Log(title = "销售数据信息", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody SalesDataBo bo) {
        return toAjax(salesDataService.insertByBo(bo));
    }

    /**
     * 修改销售数据信息
     */
    @SaCheckPermission("business:salesData:edit")
    @Log(title = "销售数据信息", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody SalesDataBo bo) {
        return toAjax(salesDataService.updateByBo(bo));
    }

    /**
     * 删除销售数据信息
     *
     * @param salesIds 主键串
     */
    @SaCheckPermission("business:salesData:remove")
    @Log(title = "销售数据信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{salesIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] salesIds) {
        return toAjax(salesDataService.deleteWithValidByIds(List.of(salesIds), true));
    }
}
